package paylayer

import (
    //"fmt"
	"strconv"
	"strings"
	//"dhfshop/utils/math"
	"dhfshop/utils/helper"
	"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/megaezgo"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)

/*
mch_id：       53941852001    (apiStoreId)
appkey：       LqvltXADZBLf   (apiStorePw)    //店家hash key
sub_mch_id:    201901041123   (barcodeSeq)
media_id：     TWD            (currency)
second_mch_id: 19C            (purposeType)		
pem_key                       (privateKey)
*/

func OrderMegaezgoPay(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	//
	apiStoreId := acPay.MchId
	apiStorePw := acPay.Appkey
	barcodeSeq := acPay.SubMchId
	currency := acPay.MediaId
	purposeType := acPay.SecondMchId
	privateKey := acPay.PemKey
	//
	ipAddr, _:= helper.GetExternal()
	orderStr := strconv.FormatInt(order.OrderId,10)
	//payment := int(math.MultiTotalPriceInt(order.Payment, 100))
	payment := int(order.Payment)
	/*
	MicroAppPay(apiStoreId,terminalIp,apiStorePw,appId,openId,orderId,barcodeSeq,
                 currency,purposeType string,amount int,privateKey []byte)(rsp *PayRsp, err error)
				 
	*/			 
					 
	megaezgoRsp, err := megaezgo.MicroAppPay(apiStoreId,ipAddr,apiStorePw,appId,openId,orderStr,barcodeSeq,currency,purposeType,payment,[]byte(privateKey))
	if err != nil {
		err = errors.Wrap(err, "megaezgo Pay MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(megaezgoRsp.PACKAGE, "prepay_id=", "", -1)
	//更新订单支付方式编号
	order.PayWayCode = 15
	shopping.UpdateOrderPayWayCode(order)
	payRsp := misc.WxResponse{}
	payRsp.AppId = megaezgoRsp.APPID
	payRsp.NonceStr = megaezgoRsp.NONCESTR
	payRsp.Package = megaezgoRsp.PACKAGE
	payRsp.SignType = megaezgoRsp.SIGN_TYPE
	payRsp.TimeStamp = megaezgoRsp.TIME_STAMP
	payRsp.PaySign = megaezgoRsp.SIGN
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderStr
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}


func ChargeMegaezgoPay(appId,openId,outTradeNo string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string,err error){
    //
    apiStoreId := acPay.MchId
	apiStorePw := acPay.Appkey
	barcodeSeq := acPay.SubMchId
	currency := acPay.MediaId
	purposeType := acPay.SecondMchId
	privateKey := acPay.PemKey
	//
	ipAddr, _:= helper.GetExternal()
	//payment := int(math.MultiTotalPriceInt(amount, 100))
	payment := int(amount)
	megaezgoRsp, err := megaezgo.MicroAppPay(apiStoreId,ipAddr,apiStorePw,appId,openId,outTradeNo,barcodeSeq,currency,purposeType,payment,[]byte(privateKey))
	if err != nil {
		err = errors.Wrap(err, "megaezgo MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(megaezgoRsp.PACKAGE, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = megaezgoRsp.APPID
	payRsp.NonceStr = megaezgoRsp.NONCESTR
	payRsp.Package = megaezgoRsp.PACKAGE
	payRsp.SignType = megaezgoRsp.SIGN_TYPE
	payRsp.TimeStamp = megaezgoRsp.TIME_STAMP
	payRsp.PaySign = megaezgoRsp.SIGN
	if tempJson, err := json.Marshal(payRsp); err == nil {
		rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonMegaezgoPay(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string, err error){
    //
	apiStoreId := acPay.MchId
	apiStorePw := acPay.Appkey
	barcodeSeq := acPay.SubMchId
	currency := acPay.MediaId
	purposeType := acPay.SecondMchId
	privateKey := acPay.PemKey
	//
	ipAddr, _:= helper.GetExternal()
	//payment := int(math.MultiTotalPriceInt(amount, 100))
	payment := int(amount)
	megaezgoRsp, err := megaezgo.MicroAppPay(apiStoreId,ipAddr,apiStorePw,appId,openId,orderId,barcodeSeq,currency,purposeType,payment,[]byte(privateKey))
	if err != nil {
		err = errors.Wrap(err, "megaezgo MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(megaezgoRsp.PACKAGE, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = megaezgoRsp.APPID
	payRsp.NonceStr = megaezgoRsp.NONCESTR
	payRsp.Package = megaezgoRsp.PACKAGE
	payRsp.SignType = megaezgoRsp.SIGN_TYPE
	payRsp.TimeStamp = megaezgoRsp.TIME_STAMP
	payRsp.PaySign = megaezgoRsp.SIGN
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
